<?php

// https://www.runoob.com/mongodb/php7-mongdb-tutorial.html

class MongoDB
{
    public $manager = null;
    public $writeConcern = null;

    public function __construct()
    {
        // 连接mongodb服务器
        if(is_null($this->manager)){
            $this->manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
        }
        if(is_null($this->writeConcern)){
            $this->writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);
        }
    }

    // 插入数据
    function write()
    {
        $document = ['_id' => new MongoDB\BSON\ObjectID, 'name' => '菜鸟教程'];

        // [BulkWrite objects may only be executed once and this instance has already been executed]
        $bulk = new MongoDB\Driver\BulkWrite;
        $_id = $bulk->insert($document);

        $result = $this->manager->executeBulkWrite('test.runoob', $bulk, $this->writeConcern);
        var_dump($result);
    }

    // 读取数据
    // 里我们将三个网址数据插入到 test 数据库的 sites 集合，并读取迭代出来
    function read()
    {
        // 批量插入数据
        $bulk = new MongoDB\Driver\BulkWrite;
        $bulk->insert(['x' => 1, 'name' => '菜鸟教程', 'url' => 'http://www.runoob.com']);
        $bulk->insert(['x' => 2, 'name' => 'Google', 'url' => 'http://www.google.com']);
        $bulk->insert(['x' => 3, 'name' => 'taobao', 'url' => 'http://www.taobao.com']);
        $this->manager->executeBulkWrite('test.sites', $bulk);

        // 过滤条件
        $filter = ['x' => ['$gt' => 1]];
        $options = [
            'projection' => ['_id' => 0],
            'sort' => ['x' => -1],
        ];

        // 查询数据
        $query = new MongoDB\Driver\Query($filter, $options);
        $cursor = $this->manager->executeQuery('test.sites', $query);

        foreach ($cursor as $document) {
            print_r($document);
        }
    }

    // 更新数据
    // 接下来我们将更新 test 数据库 sites 集合中 x 为 2 的数据：
    function update()
    {
        $bulk = new MongoDB\Driver\BulkWrite;
        $bulk->update(
            ['x' => 2],
            ['$set' => ['name' => '菜鸟工具', 'url' => 'tool.runoob.com']],
            ['multi' => false, 'upsert' => false]
        );

        $result = $this->manager->executeBulkWrite('test.sites', $bulk, $this->writeConcern);
        var_dump($result);
    }

    // 删除数据
    // 以下实例删除了 x 为 1 和 x 为 2的数据，注意 limit 参数的区别：
    function delete()
    {
        $bulk = new MongoDB\Driver\BulkWrite;
        $bulk->delete(['x' => 1], ['limit' => 1]);   // limit 为 1 时，删除第一条匹配数据
        $bulk->delete(['x' => 2], ['limit' => 0]);   // limit 为 0 时，删除所有匹配数据

        $result = $this->manager->executeBulkWrite('test.sites', $bulk, $this->writeConcern);
        var_dump($result);
    }
}

$mongodb = new MongoDB();
$mongodb->write();
$mongodb->read();
$mongodb->update();
$mongodb->delete();

// 输出结果:
// ➜  html git:(master) ✗ php app.test/public/MongoDB-class.php
// object(MongoDB\Driver\WriteResult)#7 (9) {
//   ["nInserted"]=>
//   int(1)
//   ["nMatched"]=>
//   int(0)
//   ["nModified"]=>
//   int(0)
//   ["nRemoved"]=>
//   int(0)
//   ["nUpserted"]=>
//   int(0)
//   ["upsertedIds"]=>
//   array(0) {
//   }
//   ["writeErrors"]=>
//   array(0) {
//   }
//   ["writeConcernError"]=>
//   NULL
//   ["writeConcern"]=>
//   object(MongoDB\Driver\WriteConcern)#8 (2) {
//     ["w"]=>
//     string(8) "majority"
//     ["wtimeout"]=>
//     int(1000)
//   }
// }
// stdClass Object
// (
//     [x] => 3
//     [name] => taobao
//     [url] => http://www.taobao.com
// )
// stdClass Object
// (
//     [x] => 2
//     [name] => Google
//     [url] => http://www.google.com
// )
// object(MongoDB\Driver\WriteResult)#5 (9) {
//   ["nInserted"]=>
//   int(0)
//   ["nMatched"]=>
//   int(1)
//   ["nModified"]=>
//   int(1)
//   ["nRemoved"]=>
//   int(0)
//   ["nUpserted"]=>
//   int(0)
//   ["upsertedIds"]=>
//   array(0) {
//   }
//   ["writeErrors"]=>
//   array(0) {
//   }
//   ["writeConcernError"]=>
//   NULL
//   ["writeConcern"]=>
//   object(MongoDB\Driver\WriteConcern)#6 (2) {
//     ["w"]=>
//     string(8) "majority"
//     ["wtimeout"]=>
//     int(1000)
//   }
// }
// object(MongoDB\Driver\WriteResult)#9 (9) {
//   ["nInserted"]=>
//   int(0)
//   ["nMatched"]=>
//   int(0)
//   ["nModified"]=>
//   int(0)
//   ["nRemoved"]=>
//   int(2)
//   ["nUpserted"]=>
//   int(0)
//   ["upsertedIds"]=>
//   array(0) {
//   }
//   ["writeErrors"]=>
//   array(0) {
//   }
//   ["writeConcernError"]=>
//   NULL
//   ["writeConcern"]=>
//   object(MongoDB\Driver\WriteConcern)#6 (2) {
//     ["w"]=>
//     string(8) "majority"
//     ["wtimeout"]=>
//     int(1000)
//   }
// }
// ➜  html git:(master) ✗ 